﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using FourthWallProject.GameObjects.Solids;
using FourthWallProject.GameObjects.GameComponents;

namespace FourthWallProject.GameObjects.Screens
{
    class LevelScreen : GameScreen
    {
        public List<GameObject> GameObjects{get;set;}
        public LevelGrid levelGrid;
        Character character;
        Texture2D background;
        int currentLevel = 0;

        public LevelScreen(Game game, int level)
            : base(game)
        {
            currentLevel = level;
            var texture = Game.Content.Load<Texture2D>("Images/Block");
            background = Game.Content.Load<Texture2D>("Images/Background/background");
            character = new Character(texture, new Rectangle(0, 0, 40, 40), game);
            GameObjects = new List<GameObject>();
          
            GameCamera.isFollowing = true;
            GameCamera.CameraFocus = character;

            GameCamera.WindowHeight = game.Window.ClientBounds.Height;
            GameCamera.WindowWidth = game.Window.ClientBounds.Width;

#if WINDOWS_PHONE
            GameCamera.WindowWidth = game.Window.ClientBounds.Height;
            GameCamera.WindowHeight = game.Window.ClientBounds.Width;
#endif

            base.Initialize();
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
                spriteBatch.Draw(background, new Rectangle(0, 0, GameCamera.WindowWidth, GameCamera.WindowHeight), Color.White);
            spriteBatch.End();

            levelGrid.Draw(gameTime);

            foreach (GameObject obj in GameObjects)
            {
                if (obj.Visible)
                    obj.Draw(gameTime);
            }
            spriteBatch.Begin();
            Color GradientColor = Color.Red;
            if (GameCamera.isStatic)
            {
                spriteBatch.Draw(Game.Content.Load<Texture2D>("Images/Gradient"), new Rectangle(0, GameCamera.WindowHeight, 80, GameCamera.WindowWidth), null, GradientColor, MathHelper.ToRadians(270), Vector2.Zero, new SpriteEffects(), 1);
                spriteBatch.Draw(Game.Content.Load<Texture2D>("Images/Gradient"), new Rectangle(GameCamera.WindowWidth, 0, 80, GameCamera.WindowWidth), null, GradientColor, MathHelper.ToRadians(90), Vector2.Zero, new SpriteEffects(), 1);
                spriteBatch.Draw(Game.Content.Load<Texture2D>("Images/Gradient"), new Rectangle(GameCamera.WindowWidth, GameCamera.WindowHeight, 80, GameCamera.WindowHeight), null, GradientColor, MathHelper.ToRadians(180), Vector2.Zero, new SpriteEffects(), 1);
                spriteBatch.Draw(Game.Content.Load<Texture2D>("Images/Gradient"), new Rectangle(0, 0, 80, GameCamera.WindowHeight), null, GradientColor, MathHelper.ToRadians(0), Vector2.Zero, new SpriteEffects(), 1);
            }
            spriteBatch.DrawString(GameState.MenuFont, "Camera X:" + GameCamera.X + "Camera Y:" + GameCamera.Y + "\nChar X:" + (int)character.X + " Char Y:" + (int)character.Y, Vector2.Zero, Color.Black);
            spriteBatch.End();
            base.Draw(gameTime, spriteBatch);
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            GameCamera.Update();
            
            HandleInput();
            
            foreach(GameObject obj in GameObjects)
            {
                obj.Update(gameTime);
            }

            var tile = levelGrid.CheckCollision(character);
            if (tile != null)
            {
                if(tile.SolidState == SolidState.Solid)
                    character.IsIntersected(tile);
                if (tile.SolidState == SolidState.End)
                    levelGrid.LoadLevel(levelGrid.CurrentLevel + 1);
            }
            else
            {
                if (levelGrid.CheckCollisionBelow(character))
                    character.DoFalling();
            }

            base.Update(gameTime);
        }

        private void HandleInput()
        {

            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                if (Mouse.GetState().X < GameCamera.WindowWidth / 2)
                    character.MoveLeft();
                else
                    character.MoveRight();
            }
           
            if (Keyboard.GetState().IsKeyDown(Keys.Space))
                character.Jump();
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
                character.MoveRight();
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
                character.MoveLeft();

            if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
            {
                GameCamera.isFollowing = false;
            }
        }

        protected override void LoadContent()
        {
            GameObjects.Add(character);
            GameState.Character = character;
            levelGrid = new LevelGrid(15, 15, Game);
            levelGrid.LoadLevel(currentLevel);
            character.LastPosition = character.Position;

            base.LoadContent();
        }
    }
}
